'use strict';
//获取数据库的引用
const db = uniCloud.database(); 
//获取集合的引用 表 user是数据库中的其中一个表的名字
const providecollection = db.collection('help');

exports.main = async (event, context) => {
let type = event.type;
if( type == 0){
//获取该事件信息
 let eventId = event.eventId;
 if(eventId){
	  let res = await providecollection.where({
     _id:eventId
 }).get()
 return res;
 }
}else{
	 let eventId = event.eventId;
	 let randomNumber = event.randomNumber;
	 if(eventId){
	 let provideres = await providecollection.where({
	     _id:eventId,
		 randomNumber:randomNumber,
	 }).get()
	 //若验证码符合
	 if(provideres.affectedDocs == 1){
		 //创建时间
		 const timestamp = (new Date()).getTime(); 
		 let updataRes = await providecollection.doc(eventId).update({
		   helpComplete:1,
		   completeTime:timestamp, 
		 });
		 let res = await providecollection.where({
		      _id:eventId
		 }).get()
		 return res;
	 }
	
	 return provideres;
	 }
}
};
